package com.atguigu.tingshu.order.strategy;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public interface OrderTradeStrategy {//策略接口，定义算法的抽象

    //定义构建OrderInfoVo对象的抽象方法
    OrderInfoVo trade(TradeVo tradeVo);

    // 定义获取对应的付款项目
    public abstract String getItemType() ;

    public default void setOther(OrderInfoVo orderInfoVo , TradeVo tradeVo, RedisTemplate<String,String> redisTemplate){
        // 设置其他的数据
        // 创建一个uuid作为交易编号
        String tradeNo  = UUID.randomUUID().toString().replace("-", "");
        orderInfoVo.setItemType(tradeVo.getItemType());
        orderInfoVo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);
        orderInfoVo.setTradeNo(tradeNo);   //交易编号

        //把交易号储存到Redis中
       String tradeNoRedisKey = RedisConstant.ORDER_TRADE_NO+tradeNo;
       redisTemplate.opsForValue().set(tradeNoRedisKey, tradeNo,5, TimeUnit.MINUTES);


        orderInfoVo.setTimestamp(SignHelper.getTimestamp());   // 当前系统时间

        Map<String, Object> map = JSON.parseObject(JSON.toJSONString(orderInfoVo), Map.class);
        String sign = SignHelper.getSign(map);
        orderInfoVo.setSign(sign);


    }

}
